package com.yy.permissions_general.module.system.dict.controller;

import com.yy.permissions_general.base.controller.BaseController;
import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.dict.entity.Dict;
import com.yy.permissions_general.module.system.dict.service.DictService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 字典前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-08-08
 */
@RestController
@RequestMapping("/dict")
//@RedisCache
public class DictController extends BaseController {

    @Resource
    private DictService dictService;


    /**
     * 查询列表
     *
     * @param dict
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping("/list")
    @MyLog("查询字典")
    @NoRepeatSubmit(key = "dict.list")
    public ResultUtil list(Dict dict, @RequestParam(value = "limit", defaultValue = "999999") int limit, @RequestParam(value = "page", defaultValue = "0") int page) {
        return ResultUtil.buildPagination(dictService.selectPageList(dict, limit, page));
    }

    /**
     * 新增数据
     *
     * @param dict
     * @return
     */
    @RequestMapping("/addDict")
    @MyLog("添加字典")
    @PreAuthorize("hasPermission('/dict/addDict','dict:addDict')")
    public ResultUtil addDict(Dict dict) {
        dictService.addDict(dict);
        return ResultUtil.success();
    }

    /**
     * 修改数据
     *
     * @param dict
     * @return
     */
    @RequestMapping("/editDict")
    @MyLog("修改字典")
    @PreAuthorize("hasPermission('/dict/editDict','dict:editDict')")
    public ResultUtil editDict(Dict dict) {
        dictService.editDict(dict);
        return ResultUtil.success();
    }

    /**
     * 修改状态
     *
     * @param dict
     * @return
     */
    @RequestMapping("/updateDict")
    @MyLog("修改字典状态")
    @NoRepeatSubmit(key = "dict.updateDict")
    @PreAuthorize("hasPermission('/dict/updateDict','dict:updateDict')")
    public ResultUtil updateDict(Dict dict) {
        dictService.updateDict(dict);
        return ResultUtil.success();
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    @MyLog("查询字典信息")
    public ResultUtil detail(Integer id) {
        Dict dict = dictService.getDetailsById(id);
        return ResultUtil.success(dict);
    }

    /**
     * 根据ID删除记录
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @MyLog("删除字典")
    @PreAuthorize("hasPermission('/dict/delete','dict:delete')")
    public ResultUtil delete(Integer id) {
        dictService.deleteById(id);
        return ResultUtil.success();
    }

    /**
     * 批量删除数据
     *
     * @param ids ID集合
     * @return
     */
    @RequestMapping("/deleteBatch")
    @PreAuthorize("hasPermission('/dict/deleteBatch','dict:deleteBatch')")
    @MyLog("批量删除字典")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]", required = true) List<Long> ids) {
        dictService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
